<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="用户UID">
          <el-input v-model="form.uid" placeholder="用户UID" />
        </el-form-item>
        <el-form-item label="直播间ID">
          <el-input v-model="form.video_id" placeholder="直播间ID" />
        </el-form-item>
        <el-form-item label="直播用户UID">
          <el-input v-model="form.open_uid" placeholder="直播用户UID" />
        </el-form-item>
        <el-form-item label="是否推荐">
          <el-select v-model="form.recommend">
            <el-option
              v-for="item in recommendData"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
          <!-- <el-switch v-model="form.recommend" inline-prompt active-text="是" inactive-text="否" /> -->
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onExport">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="uid" label="用户UID" align="center" />
          <el-table-column prop="platform" label="平台" align="center" />
          <el-table-column
            prop="video_id"
            label="直播ID"
            align="center"
            width="200"
          />
          <el-table-column
            prop="open_uid"
            label="直播用户UID"
            align="center"
            width="220"
          />
          <el-table-column
            prop="title"
            label="直播标题"
            align="center"
            width="220"
          />
          <el-table-column prop="online" label="是否在线" align="center" />
          <el-table-column prop="video_type" label="视频类型" align="center" />
          <el-table-column prop="room_state" label="房间状态" align="center" />
          <el-table-column label="直播间封面" align="center" width="120">
            <template #default="scope">
              <el-image
                style="width: 70px; height: 70px"
                :src="scope.row.capture"
                alt
                fit="fill"
                :preview-src-list="[scope.row.capture]"
                :preview-teleported="true"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column prop="watch_num" label="看播数" align="center" />
          <el-table-column prop="share_num" label="分享数" align="center" />
          <el-table-column prop="like_num" label="点赞数" align="center" />
          <el-table-column prop="hot_num" label="热力值" align="center" />
          <el-table-column
            prop="start_time"
            label="直播开始时间"
            align="center"
            :formatter="formatterTime"
            width="180"
          />
          <el-table-column
            prop="end_time"
            label="直播结束时间"
            align="center"
            :formatter="formatterTime"
            width="180"
          />
          <el-table-column prop="priority" label="排序" align="center" />
          <el-table-column
            prop="recommend"
            label="是否推荐"
            align="center"
            width="100"
          />
          <el-table-column
            label="操作"
            align="center"
            fixed="right"
            width="220"
          >
            <template #default="scope">
              <el-button
                type="success"
                @click="handleIsHide(scope.$index, scope.row, 'yes')"
                >推荐</el-button
              >
              <el-button
                type="info"
                @click="handleIsHide(scope.$index, scope.row, 'no')"
                >取消推荐</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";

import { formatterTime, exportToExcel } from "@/utils/utils";
import { liveRoomsApi, liveRoomsEdit } from "@/api/moby/live/live";

const tableData = ref([]);

const recommendData = [
  {
    value: "yes",
    label: "yes",
  },
  {
    value: "No",
    label: "No",
  },
];

const formData = {
  open_uid: "",
  uid: "",
  video_id: "",
  recommend: "",
};
const form = reactive({ ...formData });

const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    open_uid: form.open_uid,
    uid: form.uid,
    video_id: form.video_id,
    recommend: form.recommend,
  };
};

const loading = ref(false);

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await liveRoomsApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
  getTbaleData();
};

// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1 });
  const res = await liveRoomsApi(data);

  let head = {
    id: "ID",
    uid: "用户UID",
    platform: "平台",
    video_id: "直播间ID",
    open_uid: "直播用户UID",
    title: "直播标题",
    online: "是否在线",
    video_type: "视频类型",
    room_state: "房间状态",
    capture: "直播间封面",
    watch_num: "看播数",
    share_num: "分享数",
    like_num: "点赞数",
    hot_num: "热力值",
    start_time: "直播开始时间",
    end_time: "直播结束时间",
    priority: "排序",
    recommend: "是否推荐",
  };
  exportToExcel(res.data.results, head, "Mobybox-直播列表");
};
//上下架
const handleIsHide = (index, row, type) => {
  ElMessageBox.confirm(
    `您是否确认${type === "yes" ? "上架" : "下架"}此直播房间?`,
    "温馨提示",
    {
      type: "warning",
    }
  ).then(async () => {
    const res = await liveRoomsEdit({
      id: row.id,
      edit_type: "recommend",
      recommend: type,
    });
    getTbaleData();
    ElMessage.success(res.message);
  });
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
